package com.example.travelsn.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            //获取明文字节数组
            MessageDigest mDigest = MessageDigest.getInstance("md5");
            mDigest.update(plainText.getBytes());
            // 获得密文
            secretBytes = mDigest.digest();
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < secretBytes.length; i++) {
                int tmp = secretBytes[i];
                if (tmp < 0)
                    tmp += 256;
                if (tmp < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(tmp));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("No Such Algorithm.");
        }
    }
}


